// ignore_for_file: prefer_interpolation_to_compose_strings

import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:verse_systems/res/colors.dart';

class CounterDownPage extends StatefulWidget {
  final int countdown;
  const CounterDownPage({super.key, this.countdown = 0});

  @override
  State<CounterDownPage> createState() => _CounterDownPageState();
}

class _CounterDownPageState extends State<CounterDownPage> {
  // 用来在布局中显示相应的剩余时间
  String remainTimeStr = '';
  Timer? _timer;
  //倒计时
  void startCountDown(int time) {
    // 重新计时的时候要把之前的清除掉
    if (_timer != null) {
      if (_timer!.isActive) {
        _timer!.cancel();
        _timer = null;
      }
    }
    if (time <= 0) {
      return;
    }
    var countTime = time;
    const repeatPeriod = Duration(seconds: 1);
    _timer = Timer.periodic(repeatPeriod, (timer) {
      if (countTime <= 0) {
        timer.cancel();
        //待付款倒计时结束，可以在这里做相应的操作
        setState(() {
          remainTimeStr = "订单已失效";
        });
        return;
      }
      countTime--;
      //外面传进来的单位是秒，所以需要根据总秒数，计算小时，分钟，秒
      int hour = (countTime ~/ 3600) % 24; //如果不止24小时的就不用%24
      int minute = countTime % 3600 ~/ 60;
      int second = countTime % 60;

      // debugPrint("外面传进来的单位是秒----->hour=$hour---minute=$minute---second$second");
      var str = '';
      if (hour > 0) {
        str = '$hour小时';
      } else {
        str = '0小时';
      }
      if (minute / 10 < 1) {
        //当只有个位数时，给前面加“0”，实现效果：“:01”,":02"
        str = str + '0' + minute.toString() + "分";
      } else {
        str = "$str$minute分";
      }
      if (second / 10 < 1) {
        str = str + '0' + second.toString() + "秒";
      } else {
        str = "$str$second秒";
      }
      setState(() {
        remainTimeStr = str+"后失效";
      });
    });
  }

  @override
  void initState() {
    super.initState();
    //开始倒计时，这里传入的是秒数
    startCountDown(widget.countdown);
  }

  @override
  void dispose() {
    super.dispose();
    if (_timer != null) {
      if (_timer!.isActive) {
        _timer!.cancel();
        _timer = null;
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        Text(
          remainTimeStr.isNotEmpty ? (remainTimeStr ) : "",
          style: TextStyle(
              fontSize: 11.sp,
              color: Colours.app_main_color,
              fontWeight: FontWeight.bold),
        ),
      ],
    );
  }
}
